package com.pzr.soccer.channel.rabbit.receiver;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.pzr.soccer.channel.message.PlanMessage;
import com.pzr.soccer.channel.tio.WebSocketMessageHandler;
import com.pzr.soccer.io.email.MailService;
import com.pzr.soccer.io.email.ToEmail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.starter.TioWebSocketServerBootstrap;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author pzr
 * @date:2021-12-18-11:24
 * @Description:
 **/
@Component
@Slf4j
public class PlanMessageReceiver {
    @Autowired
    TioWebSocketServerBootstrap tioWebSocketServerBootstrap;
    @Autowired
    MailService mailService;

    @RabbitListener(queues = "Queue_Plan_Match_Message_ForWeb")
    public void processMessageForWeb(PlanMessage message){
        log.info("接收到 message for web   : " +message.getPlanName()+" "+message.getMatchId());
        Map<String,Object> pack=new HashMap<>();
        pack.put("tag", WebSocketMessageHandler.Channel_Tag_PlanMessage);
        pack.put("data",message);
        WsResponse wsResponse = WsResponse.fromText(JSON.toJSONString(pack), "utf-8");
        Tio.sendToGroup(tioWebSocketServerBootstrap.getServerTioConfig(),WebSocketMessageHandler.Channel_Group_Public, wsResponse);
    }

    @RabbitListener(queues = "Queue_Plan_Match_Message_ForMail")
    public void processMessageForMail(PlanMessage message){
       log.info("接收到 Crawler message for mail  : " + message.getPlanName()+" "+message.getMatchId());
        if(message.getEmail()==null){
            log.info(String.format("user %s has no email !",message.getUserId()));
            return;
        }
        ToEmail email=new ToEmail();
        email.setSubject("有符合的比赛");
        email.setTo(message.getEmail());
        email.setContent(formatMessageContent(message));

        //异步发送
        mailService.sendHtmlEmail(email);
    }


    private String formatMessageContent(PlanMessage message){

        String content="                                <html>\n" +
                "                                <table>\n" +
                "                                  <tr >\n" +
                "                                    <th align=\"left\" >开球时间:</th>\n" +
                "                                    <th align=\"left\">方案:</th>\n" +
                "                                     <th align=\"left\">联赛:</th>\n" +
                "                                     <th align=\"left\">主队:</th>\n" +
                "                                     <th align=\"left\">客队:</th>\n" +
                "                                     <th align=\"left\">比赛时间：</th>\n" +
                "                                     <th align=\"left\">状态:</th>\n" +
                "                                  </tr>\n" +
                "                                  <tr align=\"left\">\n" +
                "                                    <td align=\"left\">{kickOff}</td>\n" +
                "                                    <td align=\"left\">{planName}</td>\n" +
                "                                     <td align=\"left\">{league}</td>\n" +
                "                                     <td align=\"left\">{home}</td>\n" +
                "                                     <td align=\"left\">{away}</td>\n" +
                "                                     <td align=\"left\">{currentTimeStr}</td>\n" +
                "                                     <td align=\"left\">{messageType}</td>\n" +
                "                                  </tr>\n" +
                "                                </table>\n" +
                "                                </html>";
        Map<String,Object> map=new HashMap<>();
        map.put("kickOff", message.getGameKickOff());
        map.put("planName", message.getPlanName());
        map.put("league", message.getLeague());
        map.put("home",message.getHome());
        map.put("away",message.getAway());
        map.put("currentTimeStr",message.getGameCurrentTimeStr());
        map.put("messageType",message.getType());
        String formatted = StrUtil.format(content,map);
        return formatted;
    }
}
